package jn.chen.utils;
/*
 * @ClassName:NetUtil
 * @Description:
 * @author: LaoGong
 * @date: 2021/3/24
 */

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class NetUtil {
    public static void main(String[] args) {
        String url = "http://www.customs.gov.cn/customs/302249/302266/index.html";

        try {
            Document doc = Jsoup.connect(url)
                    .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36")
                    .get();

            String html = doc.html();
            System.out.println(html);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static boolean isHostConnectable(String host, int port) {
        Socket socket = new Socket();

        try {
            socket.connect(new InetSocketAddress(host, port),1000);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public static boolean isHostReachable(String host, Integer timeOut) {
        try {
            return InetAddress.getByName(host).isReachable(timeOut);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }



}
